package cn.itzd.service.impl;

import cn.itzd.entity.LoginUser;
import cn.itzd.entity.SysUser;
import cn.itzd.entity.SysUserRole;
import cn.itzd.mapper.SysUserMapper;
import cn.itzd.mapper.SysUserRoleMapper;
import cn.itzd.service.SysUserService;
import cn.itzd.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (DadaUser)表服务实现类
 *
 * @author makejava
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public List<SysUser> userList(SysUser sysUser) {
        return sysUserMapper.userList(sysUser);
    }

    @Override
    public int add(SysUser sysUser) {
        //对密码进行加密
        String encodePassword = passwordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encodePassword);
        sysUserMapper.insert(sysUser);
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUser.getId());
        sysUserRole.setRoleId(sysUser.getRoleName());
        return sysUserRoleMapper.insert(sysUserRole);
    }

    @Override
    public int edit(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
        if (sysUser.getRoleName() == null) {
            return 0;
        }
        LambdaQueryWrapper<SysUserRole> blogUserRoleQueryWrapper = new LambdaQueryWrapper<>();
        blogUserRoleQueryWrapper.eq(SysUserRole::getUserId,sysUser.getId());
        sysUserRoleMapper.delete(blogUserRoleQueryWrapper);
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUser.getId());
        sysUserRole.setRoleId(sysUser.getRoleName());
        sysUserRoleMapper.insert(sysUserRole);
        return 0;
    }

    @Override
    public int editPwd(SysUser sysUser) {
        Long userId = SecurityUtils.getUserId();
        LoginUser loginUser = sysUserMapper.queryUserById(userId);
        String password = loginUser.getPassword();
        if (!passwordEncoder.matches(sysUser.getOldPassword(),password)) {
            throw new RuntimeException("旧密码不正确！");
        }
        if (sysUser.getPassword().equals(sysUser.getOldPassword())) {
            throw new RuntimeException("新密码不能与旧密码一致！");
        }
        //对密码进行加密
        String encodePassword = passwordEncoder.encode(sysUser.getPassword());
        LambdaUpdateWrapper<SysUser> update = Wrappers.lambdaUpdate();
        update.set(SysUser::getPassword,encodePassword);
        update.eq(SysUser::getId, userId);
        return sysUserMapper.update(null,update);
    }
}

